package com.zzh.controller.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.corba.se.impl.ior.OldJIDLObjectKeyTemplate;
import com.zzh.pojo.VideoInfo;
import com.zzh.ut.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;

/*
 * @Auther : wenrouyue
 * @Date : 2022/3/8-22:33
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Slf4j
public class R {

    private int code;
    private String msg;
    private Object data;

    /**
     * 成功的返回
     *
     * @param msg  不传参指定为成功
     * @param data 不传参指定为空[]
     * @return
     */
    public static R success(Object data) {
        if (data == null) {
            data = new String[0];
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("data", data);
        log.info("查询结束,查询数据为:" + hashMap);
        return new R(RMsg.SUCCESS_CODE, RMsg.SUCCESS, hashMap);
    }

    public static R error(Integer code, String info) {

        return new R(code, RMsg.ERROR, MyUtils.getInfo(info));
    }

    public static R error(String info) {
        return new R(RMsg.ERROR_CODE, RMsg.ERROR, MyUtils.getInfo(info));
    }

    public static R error() {
        String info = "";
        return new R(RMsg.ERROR_CODE, RMsg.ERROR, MyUtils.getInfo(info));
    }

    public static R info(String info) {
        return success(MyUtils.getInfo(info));
    }

    public static R MyPage(IPage page) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("data", page.getRecords());
        hashMap.put("total", page.getTotal());
        hashMap.put("size", page.getSize());
        hashMap.put("current", page.getCurrent());
        log.info("分页查询结束,查询数据为:" + hashMap);
        return new R(RMsg.SUCCESS_CODE, RMsg.SUCCESS_PAGE, hashMap);
    }

}
